/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT
 * HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY OR
 * FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF THE SOFTWARE
 * OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS,
 * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.COPYRIGHT HOLDERS WILL NOT
 * BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL OR CONSEQUENTIAL
 * DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE OR DOCUMENTATION.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://gnu.org/licenses/>.
 */
 
 /**
 * popDropdown jQuery Plugin
 *  
 * @author		Manuel Diego Paillafil Gamboa
 * @link		http://manuel.radiohead.cl/
 * @email		manuel@radiohead.cl
 * 
 * @file		jquery.popdropdown.js
 * @version		1.0
 * @date		09/10/2009
 * 
 */
jQuery.popdropdown = function(elements,setting) 
{
	settings = jQuery.extend(
		{
			after : function(op){},
			before : function(op){},
			blockUI : false
		} , setting);
		
        jQuery.popdropdownfill = function(data,target) {
					$(target).html("");
					data = eval(data);
					for (i = 0; i < data.length; i++){
			  			for ( key in data[i] ){	
							$(target).get(0).add(new Option(data[i][key],[key]), document.all ? i : null);
              			}
					}
				};
        jQuery.popdropdownbefore = function(message,target_form) {
        	settings.before();
			target_form.block({  
				message: '<span class="negrita quince">'+message+'</span>',
				css: {border: 'none', 
				padding: '15px', 
				backgroundColor: '#000', 
					'-webkit-border-radius': '10px', 
					'-moz-border-radius': '10px', 
					opacity: '.5', 
					color: '#fff' 
                 				}     
          				});
		};
		jQuery.popdropdownafter = function(message,target_form) {
			settings.after();
			target_form.unblock({
				fadeOut: 800 
			});
		};
        
	$.each(elements, function(key,val){
		if((key == 0) && (settings.blockUI != false)){
			target_form = $(val.target).parents('form');
		}		
		jQuery.ajax({
   			type: "GET",
   			url: val.url,
   			data: {'_value' : val.value},
   			beforeSend: function(){
				if((key == 0) && (settings.blockUI != false)){
					jQuery.popdropdownbefore(settings.blockUI,target_form);
				}
			},
   			success: function(data){
				jQuery.popdropdownfill(data,val.target);
				
				if((elements.length == key+1) && (settings.blockUI != false)){
					jQuery.popdropdownafter(settings.blockUI,target_form);
				}
   			}
 		});
	});
};